package com.lg.partition34;

import java.io.*;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;

/**
 * @author `RKC`
 * @date 2022/3/23 10:15
 */
public class P3371模板_单源最短路径_弱化版 {

    private static final int N = 10010, M = 500010;
    private static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    private static int[] dist = new int[N];
    private static Queue<Integer> q = new ArrayDeque<>(N);
    private static boolean[] st = new boolean[N];
    private static int n, m, idx = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        n = Integer.parseInt(ss[0]);
        m = Integer.parseInt(ss[1]);
        int start = Integer.parseInt(ss[2]);
        Arrays.fill(h, -1);
        Arrays.fill(dist, Integer.MAX_VALUE);
        for (int i = 0; i < m; i++) {
            ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]), c = Integer.parseInt(ss[2]);
            add(a, b, c);
        }
        spfa(start);
        for (int i = 1; i <= n; i++) writer.write(dist[i] + " ");
        writer.flush();
    }

    private static void spfa(int start) {
        q.add(start);
        st[start] = true;
        dist[start] = 0;
        while (!q.isEmpty()) {
            int u = q.poll();
            st[u] = false;
            for (int i = h[u]; i != -1; i = ne[i]) {
                int v = e[i];
                if (dist[v] > dist[u] + w[i]) {
                    dist[v] = dist[u] + w[i];
                    if (st[v]) continue;
                    q.add(v);
                    st[v] = true;
                }
            }
        }
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        w[idx++] = c;
    }
}
